﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using Fiddler;
using Hardanger.ViewModel;
using Hardanger.Model;
using Hardanger.Utils;
using System.Collections.ObjectModel;

namespace HardangerStandalone
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window

    {
        public MainWindow()
        {
            InitializeComponent();            
			this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
			this.Unloaded += new RoutedEventHandler(MainWindow_Unloaded);
			this.Closing += new CancelEventHandler(MainWindow_Closing);
        }
		void MainWindow_Closing(object sender, CancelEventArgs e)
		{
			TerminateFiddler();
		}

		void MainWindow_Unloaded(object sender, RoutedEventArgs e)
        {
			TerminateFiddler();
        }

		private static void TerminateFiddler()
		{
			FiddlerApplication.oProxy.Detach();
		}

		void  MainWindow_Loaded(object sender, RoutedEventArgs e)
		{
            this.DataContext = new HardangerViewModel();
            int port;
            var result = Int32.TryParse(localPort.Text, out port);
			if (!CertMaker.rootCertExists())
			{
				CertMaker.createRootCert();
			}
			if(!CertMaker.rootCertIsTrusted())
			{
				CertMaker.trustRootCert();
			}
            FiddlerApplication.Startup(result == true ? port : 37337, FiddlerCoreStartupFlags.RegisterAsSystemProxy | FiddlerCoreStartupFlags.DecryptSSL | FiddlerCoreStartupFlags.MonitorAllConnections);
            FiddlerApplication.BeforeRequest += new SessionStateHandler(FiddlerApplication_BeforeRequest);			
		}
        
        /// <summary>
        /// Main Entry point for new web requests.
        /// </summary>
        /// <param name="oSession"></param>
        void FiddlerApplication_BeforeRequest(Session oSession)
        {
			// Skipping CONNECT SSL tunnels for now since we don't support fuzzing them yet
			if (!oSession.isTunnel)
			{
                //Converts the FiddlerSession to a HardangerSession then adds to the SessionManager
				SessionManager.Current.AddSession(FiddlerSessionParser.ParseFiddlerSession(oSession.id, oSession.hostname, oSession.port, oSession.isHTTPS, oSession.ToString()));
			}
        }


        private void ProxyToggle_Click(object sender, RoutedEventArgs e)
        {
            if (!FiddlerApplication.oProxy.IsAttached)
            {
                localPort.IsEnabled = false;
                FiddlerApplication.oProxy.Attach();
                ProxyToggle.Content = "Stop Capture";
            }
            else
            {
                localPort.IsEnabled = true;
                FiddlerApplication.oProxy.Detach();
                ProxyToggle.Content = "Start Capture";
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SessionManager.Current.AllSessions.Clear();
        }
    }     
 }

